package jgrit.options;

/**
 * Option that holds a true/false value.
 * 
 * @author Derek Olejnik
 */
public class BooleanOption extends Option {
	public static final String TRUE = "true";
	public static final String FALSE = "false";
	
	public BooleanOption(String name, String group, boolean initialValue) {
		super(name, group, initialValue ? TRUE : FALSE);
	}
	
	/**
	 * @deprecated use {@link #BooleanOption(String, String, boolean)} instead.
	 */
	@Deprecated
	public BooleanOption(String name, String group, String initialValue) {
		super(name, group, initialValue);
	}
	
	/**
	 * Convenience value setter that takes a boolean instead of a string.
	 */
	public boolean setValue(boolean newValue) {
		return setValue(newValue ? TRUE : FALSE);
	}

	/**
	 * Will refuse to set an illegal (non-boolean) string value.
	 * This is contrary to the superclass API for this method.
	 */
	@Override
	public boolean setValue(String newValue) {
		// We only want to set this item to true/false
		if (!newValue.equalsIgnoreCase(TRUE) && !newValue.equalsIgnoreCase(FALSE)) {
			return false;
		} else {
			return super.setValue(newValue.toLowerCase());
		}
	}
	
	public boolean getBooleanValue() {
		return getValue().equalsIgnoreCase(TRUE);
	}
}
